using System;
using System.ComponentModel.Design;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WebServiceTool.Services;
using WebServiceTool.Utilities;
using WebServiceTool.View;
using WebServiceTool.ViewModels;

namespace WebServiceTool
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {

        protected override void OnStartup(StartupEventArgs e)
        {
            
            var host = Host.CreateDefaultBuilder()
                .ConfigureLogging(builder => { builder.AddConsole(); }
                )
                .ConfigureServices((context, services) =>
                {
                    services.AddSingleton<MainWindow>();
                    services.AddSingleton<MainWindowViewModel>();
                    services.AddHttpClient();
                    services.AddSingleton<WsdlParsingHelper>();
                    services.AddSingleton<IWebService, WebService>();
                })
                .Build();

            var serviceProvider = host.Services;

            try
            {
                var mainWindow = serviceProvider.GetRequiredService<MainWindow>();
                mainWindow.Show();
            }
            catch (Exception ex)
            {
                // 记录启动异常并适当处理
                var logger = serviceProvider.GetService<ILogger<MainWindow>>();
                logger?.LogError(ex, "Failed to start main window");
                throw;
            }

            base.OnStartup(e);
        }


    }


}